#include <stdio.h>
#include <stdlib.h>

#ifndef Point_TypeDef_def
#define Point_TypeDef_def
typedef struct
{
	double* Val ;
	unsigned int Axis ;
}Point_TypeDef;
#endif

#ifndef ResStatus_TypeDef_def
#define ResStatus_TypeDef_def
typedef enum
{ 
    Error		= 0x00, 	
    Success		= 0x01 	
}ResStatus_TypeDef;
#endif

Point_TypeDef My_PointCreate(unsigned int Axis, double* Val)
{
	Point_TypeDef point;
	point.Axis=Axis;
	point.Val=(double*)malloc(point.Axis*sizeof(double));
	for(unsigned int i=0;i<Axis;i++)
		point.Val[i]=Val[i];
	return point;
}

void My_PointFree(Point_TypeDef* input)
{
	free(input->Val);
}

ResStatus_TypeDef My_LinearInterpolation(Point_TypeDef* input1, Point_TypeDef* input2, unsigned int axis, double targetValue, Point_TypeDef* output)
{
	if(input1->Axis != input2->Axis)
		return Error;
	if(input1->Val[axis]==input2->Val[axis])
		return Error;
	double t = (targetValue - input1->Val[axis]) / (input2->Val[axis] - input1->Val[axis]);
	output->Val = (double*)malloc(input1->Axis*sizeof(double));
	output->Axis = input1->Axis;
	for (unsigned int i = 0; i < input1->Axis; ++i) 
	{
		output->Val[i] = input1->Val[i] + t * (input2->Val[i] - input1->Val[i]);
	}
	return Success;
}

int main(void)
{
    double p1Data[] = {1.0, 2.0, 3.0, 4.0};
    double p2Data[] = {4.0, 5.0, 6.0, 8.0};

    Point_TypeDef p1 = My_PointCreate(4, p1Data);
    Point_TypeDef p2 = My_PointCreate(4, p2Data);

    Point_TypeDef result;

    ResStatus_TypeDef status = My_LinearInterpolation(&p1, &p2, 0, 3, &result);

    if (status == Success ) {
        printf("插值结果: [%f, %f, %f, %f]\n",
               result.Val[0], result.Val[1], result.Val[2], result.Val[3]);
        My_PointFree(&result);
    } else {
        printf("插值失败。\n");
    }

    // 释放原始点
    My_PointFree(&p1);
    My_PointFree(&p2);

    return 0;
}

